package cpr.command;

import java.util.Iterator;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.Rule;
import cpr.context.Context;
import cpr.context.SolveContext;
import cpr.context.WorksetContext;
import cpr.data.SolveBestRulesEntry;

public class CmdSolutionTest extends DefaultCommandHandler {
	public CmdSolutionTest() {
		super("sol-test","sol-test ruleId", null,null);
	}
	
	private int counter_ok = 0;
	private int counter_not_ok = 0;
	private int counter_not_cov = 0;
	
	public void execute(String[] params) throws Exception {
		counter_ok = 0;
		counter_not_ok = 0;
		counter_not_cov = 0;
		
		int ruleId = getParameterInteger(params, 1,-1);
		Context ctx = getCommandContext().getContext();
		SolveContext sctx = ctx.assertCurrentSolveContext();
		SolveBestRulesEntry solution = sctx.getSolution();

		if (ruleId<0) {
			for (Iterator iter = sctx.hl_rules.iterator(); iter.hasNext();) {
				Rule aRule = (Rule) iter.next();
				testRule(aRule);
			}
			printTestResults();
		} else {
			Rule aRule = (Rule)sctx.hl_rules.get(ruleId);
			testRule(aRule);
			int lastARE = sctx.getLastMatchingAREntry(solution, aRule);
			Printer.println("covering rule with max score: = "+lastARE);

		}
		
		
	}
	private void testRule(Rule aRule) {
		Context ctx = getCommandContext().getContext();
		SolveContext sctx = ctx.assertCurrentSolveContext();
		SolveBestRulesEntry solution = sctx.getSolution();
		
		Boolean ok = sctx.checkSolution(solution, aRule);
		if (ok == null) {
			counter_not_cov++;
			Printer.println(aRule.ruleNr+". "+"NOT COVERED");
		} else if (ok.booleanValue()) {
			counter_ok++;
			Printer.println(aRule.ruleNr+". "+"OK");
		} else {
			counter_not_ok++;
			Printer.println(aRule.ruleNr+". "+"NOT OK");
		}
	}
	
	private void printTestResults() {
		Printer.println("OK\t\t"+counter_ok);
		Printer.println("NOT OK\t\t"+counter_not_ok);
		Printer.println("NOT COVERED\t"+counter_not_cov);
		
	}
 }
